package com.bank.controller;


import com.bank.auto.JSONAuthentication;
import com.bank.entity.BaMeans;
import com.bank.entity.BaRepay;
import com.bank.entity.BaUser;
import com.bank.mapper.BaMeansMapper;
import com.bank.mapper.BaUserMapper;
import com.bank.response.Result;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author bank
 * @since 2022-03-16
 */
@RestController
@RequestMapping("/ba-means")
public class BaMeansController extends JSONAuthentication {
    @Autowired
    BaMeansMapper baMeansMapper;
    @Autowired
    BaUserMapper baUserMapper;

    @ApiOperation(value = "添加用户的资料")
    @PostMapping("/add")
    public void addCar(HttpServletRequest request, HttpServletResponse response, @RequestBody BaMeans baMeans) throws Exception {
      QueryWrapper wrapper=new QueryWrapper();
      wrapper.eq("user_id",baMeans.getUserId());
      BaMeans baMeans1=baMeansMapper.selectOne(wrapper);
      if(baMeans1!=null){
          Result error = Result.error().message("用户资料已存在，不可添加");
          this.WriteJSON(request, response, error);
      }else{
          QueryWrapper wrapper1=new QueryWrapper();
          wrapper1.eq("count",baMeans.getCount());
          BaMeans baMeans2=baMeansMapper.selectOne(wrapper1);
          if(baMeans2!=null){
              Result error = Result.error().message("该账号已存在，请核实后添加");
              this.WriteJSON(request, response, error);
          }else{
              String pswd = new BCryptPasswordEncoder().encode(baMeans.getDrawPassword());
              baMeans.setDrawPassword(pswd);
              if (baMeansMapper.insert(baMeans) > 0) {
                  Result ok = Result.ok();
                  this.WriteJSON(request, response, ok);
              } else {
                  Result error = Result.error();
                  this.WriteJSON(request, response, error);
              }
          }
      }
    }

    @ApiOperation(value = "删除用户资料")
    @PostMapping("/del")
    public void delUser(HttpServletRequest request, HttpServletResponse response, @RequestParam("id") int id) throws Exception {
        int i = baMeansMapper.deleteById(id);
        if (i > 0) {
            Result ok = Result.ok();
            this.WriteJSON(request, response, ok);
        } else {
            Result error = Result.error();
            this.WriteJSON(request, response, error);
        }
    }
    @ApiOperation(value = "修改用户资料状态(通过)")
    @PostMapping("/updMeanStatus")
    public void updMeanStatus(HttpServletRequest request, HttpServletResponse response,@RequestParam("id") int id) throws Exception {
        BaMeans baMeans=baMeansMapper.selectById(id);
        baMeans.setStatus("通过");
        if(baMeansMapper.updateById(baMeans)>0){
            Result ok = Result.ok();
            this.WriteJSON(request, response, ok);
        }else{
            Result error = Result.error();
            this.WriteJSON(request, response, error);
        }
    }
    @ApiOperation(value = "修改用户资料状态(不通过)")
    @PostMapping("/updMeanStatusNo")
    public void updMeanStatusNo(HttpServletRequest request, HttpServletResponse response,@RequestParam("id") int id) throws Exception {
        BaMeans baMeans=baMeansMapper.selectById(id);
        baMeans.setStatus("不通过");
        if(baMeansMapper.updateById(baMeans)>0){
            Result ok = Result.ok();
            this.WriteJSON(request, response, ok);
        }else{
            Result error = Result.error();
            this.WriteJSON(request, response, error);
        }
    }

    @ApiOperation(value = "修改取钱密码")
    @PostMapping("/updPassword")
    public void updUser(HttpServletRequest request, HttpServletResponse response, @RequestParam("password") String password, @RequestParam("rpassword") String rpassword, @RequestParam("id") int id) throws Exception {
        BaMeans baMeans = baMeansMapper.selectById(id);
        BCryptPasswordEncoder encoder = new BCryptPasswordEncoder();
        if (encoder.matches(password, baMeans.getDrawPassword())) {
            String newPassword = new BCryptPasswordEncoder().encode(rpassword);
            baMeans.setDrawPassword(newPassword);
            if (baMeansMapper.updateById(baMeans) > 0) {
                Result ok = Result.ok();
                this.WriteJSON(request, response, ok);
            } else {
                Result error = Result.error();
                this.WriteJSON(request, response, error);
            }
        } else {
            Result error = Result.error();
            error.setMessage("密码不正确");
            this.WriteJSON(request, response, error);
        }
    }

    @ApiOperation(value = "根据用户id查询资料")
    @PostMapping("/findById")
    public void findById(HttpServletRequest request, HttpServletResponse response, @RequestParam("userId") int userId) throws Exception {
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.like("user_id", userId);
        BaMeans baMeans = baMeansMapper.selectOne(wrapper);
        if (baMeans != null) {
            Result ok = Result.ok().data("mean", baMeans);
            this.WriteJSON(request, response, ok);
        } else {
            Result error = Result.error();
            this.WriteJSON(request, response, error);
        }
    }
    @ApiOperation(value = "查询全部待审核的资料")
    @PostMapping("/findByIdSh")
    public void findByIdSh(HttpServletRequest request, HttpServletResponse response,  @RequestParam("size") int size, @RequestParam("current") int current) throws Exception {
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.like("status", "待审核");
       Page<BaMeans> page=new Page<>(current,size);
       Page<BaMeans> page1=baMeansMapper.selectPage(page,wrapper);
       List<BaMeans> list=page1.getRecords();
       int total=baMeansMapper.selectCount(wrapper);
        if (list.size()>0) {
            Result ok = Result.ok().data("mean", list).data("total",total);
            this.WriteJSON(request, response, ok);
        } else {
            Result error = Result.error().message("暂无申请");
            this.WriteJSON(request, response, error);
        }
    }
    @ApiOperation(value = "查询全部资料")
    @PostMapping("/findByIdAll")
    public void findByIdAll(HttpServletRequest request, HttpServletResponse response,  @RequestParam("size") int size, @RequestParam("current") int current) throws Exception {
        Page<BaMeans> page=new Page<>(current,size);
        Page<BaMeans> page1=baMeansMapper.selectPage(page,null);
        List<BaMeans> list=page1.getRecords();
        int total=baMeansMapper.selectCount(null);
        if (list.size()>0) {
            Result ok = Result.ok().data("mean", list).data("total",total);
            this.WriteJSON(request, response, ok);
        } else {
            Result error = Result.error().message("暂无申请");
            this.WriteJSON(request, response, error);
        }
    }
}
